package tests;

import static org.junit.Assert.assertEquals;
import model.components.ComponentDoor;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import utils.Constants;
import controller.ComponentDoorController;
import controller.EcluseController;
import controller.FakeNetwork;
import controller.SensorDoorController;
import exception.ExceptionEcluse;

public class EcluseControllerTest {

	public FakeNetwork fnet;
	public EcluseController ec;

	@Before
	public void setUp() throws Exception {
		fnet = new FakeNetwork();
		ec = new EcluseController(Constants.ECLUSE_PASSIVE_MODE, fnet);
			
		ec.addComponent(new ComponentDoorController("Door 1",  Constants.COMPONENT_DOOR_CLOSED));
		ec.addComponent(new SensorDoorController("Sensor Door 1",  0, ec.getLastAddedComponent()));
		ec.addComponent(new ComponentDoorController("Door 2",  Constants.COMPONENT_DOOR_CLOSED));
		ec.addComponent(new SensorDoorController("Sensor Door 2",  0, ec.getLastAddedComponent()));
		ec.addComponent(new ComponentDoorController("Door 3",  Constants.COMPONENT_DOOR_CLOSED));
		ec.addComponent(new SensorDoorController("Sensor Door 3",  0, ec.getLastAddedComponent()));
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	/**
	 * Test to verify that opening and closing doors work correctly
	 */
	public void openAndCloseDoors() {

		((ComponentDoor) fnet.getComponentByLabel("Door 1")).isOpened();

		assertEquals((boolean) false, ((ComponentDoor) fnet.getComponentByLabel("Door 1")).isOpened());
		assertEquals((boolean) false, ((ComponentDoor) fnet.getComponentByLabel("Door 2")).isOpened());

		((ComponentDoor) fnet.getComponentByLabel("Door 1")).setOpened(true);

		assertEquals((boolean) true, ((ComponentDoor) fnet.getComponentByLabel("Door 1")).isOpened());
		assertEquals((boolean) false, ((ComponentDoor) fnet.getComponentByLabel("Door 2")).isOpened());

		((ComponentDoor) fnet.getComponentByLabel("Door 2")).setOpened(true);

		assertEquals((boolean) true, ((ComponentDoor) fnet.getComponentByLabel("Door 1")).isOpened());
		assertEquals((boolean) true, ((ComponentDoor) fnet.getComponentByLabel("Door 2")).isOpened());

	}

	@Test
	/**
	 * Test the load of configuration
	 */
	public void initialize() throws ExceptionEcluse {
		ec.initialize();
	}
}
